package com.xy.manage.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import sun.misc.BASE64Encoder;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Talarm;
import com.xy.manage.service.TalarmService;

@Controller
public class TalarmAction {
	private static final Logger log = LoggerFactory
			.getLogger(TalarmAction.class);

	public TalarmAction() {
	}

	/**
	 * 一键报警新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_talarmAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("talarm", new Talarm());
		setDict(model);
		return "talarmEdit";
	}

	/**
	 * 一键报警新增保存
	 * 
	 * @param talarm
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_talarmSave.do")
	public String save(Talarm talarm, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(talarm, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		talarm = talarmService.save(talarm);
		return "redirect:v_talarmList.do";
	}

	/**
	 * 一键报警删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_talarmDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				talarmService.remove(id);
			}
		}
		return "redirect:v_talarmList.do";
	}

	/**
	 * 一键报警编辑页面
	 * 
	 * @param alarmid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_talarmEdit.do")
	public String edit(long alarmid, HttpServletRequest request, ModelMap model) {
		Talarm talarm = talarmService.get(alarmid);
		model.addAttribute("talarm", talarm);
		model.addAttribute("actionEdit", request.getParameter("actionEdit"));
		setDict(model);
		return "talarmEdit";
	}

	/**
	 * 一键报警详情
	 * 
	 * @param alarmid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_talarmDetail.do")
	public String detail(long alarmid, HttpServletRequest request,
			ModelMap model) {
		Talarm talarm = talarmService.get(alarmid);
		model.addAttribute("talarm", talarm);
		setDict(model);
		String pic64 = Base64.encode(talarm.getPic());
		String img = "data:image/png;base64," + pic64;
		model.addAttribute("imgSource", img);
		return "talarmView";
	}

	/**
	 * 一键报警查询（分页）
	 * 
	 * @param talarm
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_talarmList.do")
	public String listPage(Talarm talarm, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = talarmService.getTalarmByPage(toMap(talarm),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
		setDict(model);
		return "talarmList";
	}

	/**
	 * 一键报警查询
	 * 
	 * @param talarm
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_talarmList1.do")
	public String list(Talarm talarm, HttpServletRequest request, ModelMap model) {
		List<Talarm> list = talarmService.getTalarm(toMap(talarm));
		model.addAttribute("list", list);
		setDict(model);
		return "talarmList";
	}

	/***
	 * 将页面输入信息转成map
	 * 
	 * @param talarm
	 * @return
	 */
	private Map<String, Object> toMap(Talarm talarm) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (talarm.getAlarmid() != null && !talarm.getAlarmid().equals("")) {
			map.put("Alarmid", talarm.getAlarmid());
		}
		if (talarm.getUsername() != null && !talarm.getUsername().equals("")) {
			map.put("Username", talarm.getUsername());
		}
		if (talarm.getContent() != null && !talarm.getContent().equals("")) {
			map.put("Content", talarm.getContent());
		}
		if (talarm.getPhoneNo() != null && !talarm.getPhoneNo().equals("")) {
			map.put("PhoneNo", talarm.getPhoneNo());
		}
		if (talarm.getPosition() != null && !talarm.getPosition().equals("")) {
			map.put("Position", talarm.getPosition());
		}
		if (talarm.getPic() != null && !talarm.getPic().equals("")) {
			map.put("Pic", talarm.getPic());
		}
		if (talarm.getCreatetime() != null
				&& !talarm.getCreatetime().equals("")) {
			map.put("Createtime", talarm.getCreatetime());
		}
		return map;
	}

	/**
	 * 设置页面所需的数据字典
	 * 
	 * @param model
	 */
	private void setDict(ModelMap model) {

	}

	/**
	 * 数据校验
	 * 
	 * @param model
	 */
	private WebErrors validateSave(Talarm talarm, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**
		 * if (errors.ifOutOfLength(username, "username", 1, 100)) { return
		 * errors; }
		 */
		return errors;
	}

	@Resource
	private TalarmService talarmService;
}